********************************************************************************
/* Survey analysis */
********************************************************************************
version 13
clear all
set matsize 5000
set memory 200m
set more off

use "2_survey_data.dta", clear


********************************************************************************
/* conjoint - reshape */
********************************************************************************
*load data and reshape to scenario level data							 
*keep  uniqueID cj1_* cj2_* cj3_* cj4_*
order uniqueID

*reshape to scenarios
reshape long cj1_FeatHHInc cj1_FeatDamage cj1_FeatDead cj1_FeatPresVote cj1_FeatUnemp cj1_FeatPresident cj1_relief ///
             cj2_FeatHHInc cj2_FeatDamage cj2_FeatDead cj2_FeatPresVote cj2_FeatUnemp cj2_FeatPresident cj2_relief ///
			 cj3_FeatHHInc cj3_FeatDamage cj3_FeatDead cj3_FeatPresVote cj3_FeatUnemp cj3_FeatPresident cj3_relief ///
             cj4_FeatHHInc cj4_FeatDamage cj4_FeatDead cj4_FeatPresVote cj4_FeatUnemp cj4_FeatPresident cj4_relief ///
			 ,i(uniqueID) j(scenario)						 

forvalues i = 1/4 {
renvars cj`i'_* , postfix(_`i')
renvars cj`i'_* , predrop(4)
}			 
			 
reshape long FeatHHInc_ FeatDamage_ FeatDead_ FeatPresVote_ FeatUnemp_ FeatPresident_ relief_, i(uniqueID scenario)	

rename _j conjointno
order uniqueID conjointno scenario 
sort  uniqueID conjointno scenario 
egen contestid = group(uniqueID conjointno)


*create categorical variables
egen FeatHHInc = cut(FeatHHInc_), at(10000,40000,70000,100000,100001) label
egen FeatDamage = cut(FeatDamage_), at(0.1,1,4,24,25) label
egen FeatDead = cut(FeatDead_), at(0,5,6) label
egen FeatUnemp = cut(FeatUnemp_), at(3,5,7,9,10) label
rename FeatPresVote_ FeatPresVote

gen log_relief = log(relief_+1)

drop FeatHHInc_ FeatDamage_ FeatDead_ FeatUnemp_ 

egen test_con = sum(relief_), by(uniqueID conjointno)
tab test_con

gen inc10 = (FeatHHInc==0)
gen inc40 = (FeatHHInc==1)
gen inc70 = (FeatHHInc==2)
gen inc100 = (FeatHHInc==3)


/* ebal */
* Age (same categories we used as target for the sample)
gen agegr18_24 = (age > 17 & age <25)
gen agegr25_44 = (age > 24 & age < 45)
gen agegr45_64 = (age > 44 & age < 65)
gen agegr65_o = (age > 64 & age < 150)

* Voting population age
gen a18to24 = age < 25
gen a25to29 = age < 30 & age > 24
gen a30to39 = age > 29 & age < 40
gen a40to49 = age > 39 & age < 50
gen a50to64 = age > 49 & age < 65
gen a65plus = age > 64

* Voting population education
gen highSchoolOrLess = education < 5
gen college = education == 7 | education == 8 | education == 9
gen postgraduate = education == 10

* Edcuation (same categories we used as target for the sample)
gen lessThanHighSchool = (education > 0 & education < 4)
gen highSchoolDegree = education == 4
gen someCollege = (education > 4 & education < 7)
gen bachelor = education == 7
gen advancedDegree = (education > 7 & education < 11)


foreach var of varlist agegr18_24-agegr65_o lessThanHighSchool-advancedDegree {
tab `var'
}

* Now we run the ebalance command to reweight the sample
* Target data
ebalance agegr18_24 agegr25_44 agegr45_64 sex lessThanHighSchool highSchoolDegree someCollege bachelor, manualtargets(.126 .342 .339 .487 .122 .296 .284 .192)

* Rename weighting variable
rename _webal weight

* Weight for voting population
ebalance a18to24 a25to29 a30to39 a40to49 a50to64 highSchoolOrLess someCollege college sex, manualtargets(.10 .09 .17 .19 .30 .18 .32 .32 .47)
rename _webal weightLikelyVoter

* Set dataset as survey dataset with weights
svyset [pweight = weight]


********************************************************************************
/* conjoint - regression analysis */
********************************************************************************

/* Main section results */
*Figure 2: Democratic vs. Republican president
reg relief_ i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote [pweight = weight] if FeatPresident == 1,  cluster(uniqueID) robust 
matrix pres_dem = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(pres_dem) saving(olspres_dem) replace

reg relief_ i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote [pweight = weight] if FeatPresident == 0,  cluster(uniqueID) robust 
matrix pres_rep = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(pres_rep) saving(olspres_rep) replace

*Figure 3: Affectedness-based fairness: High vs. Low
reg relief_ i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote [pweight = weight] if aff_norm_high == 1,  cluster(uniqueID) robust  
matrix aff_norm_a = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(aff_norm_a) saving(olsaff_norm_a) replace

reg relief_ i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote [pweight = weight] if aff_norm_high == 0,  cluster(uniqueID) robust  
matrix aff_norm_d = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(aff_norm_d) saving(olsaff_norm_d) replace

reg relief_ i.FeatDamage##i.aff_norm_high i.FeatDead##i.aff_norm_high inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote [pweight = weight],  cluster(uniqueID) robust
matrix aff_diff = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(aff_diff) saving(olsaff_diff) replace

putexcel A1=matrix(aff_diff, names) using olsaff_diff, replace

*Figure 4: Need-based fairness: High vs. Low
sum need_norm, det
reg relief_ i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote  [pweight = weight] if need_norm_high == 1,  cluster(uniqueID) robust  
matrix need_norm_a = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(need_norm_a) saving(olsneed_norm_a) replace

reg relief_ i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote [pweight = weight] if need_norm_high == 0,  cluster(uniqueID) robust  
matrix need_norm_d = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(need_norm_d) saving(olsneed_norm_d) replace

reg relief_ i.FeatDamage i.FeatDead i.inc70##i.need_norm_high i.inc40##i.need_norm_high i.inc10##i.need_norm_high i.FeatUnemp##i.need_norm_high i.FeatPresVote [pweight = weight],  cluster(uniqueID) robust
matrix need_diff = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(need_diff) saving(olsneed_diff) replace
putexcel A1=matrix(need_diff, names) using olsneed_diff, replace

*Figure 5: Electorally based fairness: High vs. Low
*Democratic president
reg relief_ i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote [pweight = weight] if FeatPresident == 1 & gvt_prio_high == 1,  cluster(uniqueID) robust  
matrix ols_dem_high = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(ols_dem_high) saving(ols_dem_high) replace

reg relief_ i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote [pweight = weight] if FeatPresident == 1 & gvt_prio_high == 0,  cluster(uniqueID) robust 
matrix ols_dem_low = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(ols_dem_low) saving(ols_dem_low) replace

reg relief_ i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote##i.gvt_prio_high [pweight = weight],  cluster(uniqueID) robust 
matrix gvt_prio_diff= (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(gvt_prio_diff) saving(olselec_fair_diff) replace
putexcel A1=matrix(gvt_prio_diff, names) using olselec_fair_diff, replace

*Republican president
reg relief_ i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote [pweight = weight] if FeatPresident == 0 & gvt_prio_high == 1,  cluster(uniqueID) robust  
matrix ols_rep_high = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(ols_rep_high) saving(ols_rep_high) replace

reg relief_ i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote [pweight = weight] if FeatPresident == 0 & gvt_prio_high == 0,  cluster(uniqueID) robust 
matrix ols_rep_low = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(ols_rep_low) saving(ols_rep_low) replace

reg relief_ i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote##i.gvt_prio_high [pweight = weight],  cluster(uniqueID) robust 
matrix gvt_prio_diff1 = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(gvt_prio_diff1) saving(olselec_fair_diff1) replace
putexcel A1=matrix(gvt_prio_diff1, names) using olselec_fair_diff1, replace


*Figure 6: Partisan identification and government partisanship 
*president: democrat, partyID: democrat
reg relief_ i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote [pweight = weight] if FeatPresident == 1 & partyID ==2,  cluster(uniqueID) robust 
matrix pres_dem_dem = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(pres_dem_dem) saving(olspres_dem_dem) replace

*president: republican, partyID: republican
reg relief_ i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote [pweight = weight] if FeatPresident == 0 & partyID ==1,  cluster(uniqueID) robust 
matrix pres_rep_rep = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(pres_rep_rep) saving(olspres_rep_rep) replace

*Figure 7: Preferred allocation
reg log_relief i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote [pweight = weight],  cluster(uniqueID) robust 
matrix main = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(main) saving(olscon_main_log) replace


********************************************************************************
/* Appendix results */
*Table A1: Affectedness
*Myself
gen aff_myself = roads_m+school_m+comm_m+food_m+evacuation_m+finloss_m+injury_m					
tab aff_myself //not affected = 0
tab roads_m
tab school_m
tab comm_m
tab food_m
tab evacuation_m
tab finloss_m
tab injury_m

*Other household members
gen aff_household = roads_h+school_h+comm_h+food_h+evacuation_h+finloss_h+injury_h
tab aff_household //not affected = 0					
tab roads_h
tab school_h
tab comm_h
tab food_h
tab evacuation_h
tab finloss_h
tab injury_h

*Table A2: Fairness norms
*Affectedness
sum aff_norm //Mean, Std. Dev, Min, Max
sum aff_norm, det //Median

*Need
sum need_norm //Mean, Std. Dev, Min, Max
sum need_norm, det //Median

*Electoral Reciprocity
sum gvt_prio //Mean, Std. Dev, Min, Max
sum gvt_prio, det //Median

*Table A3: Population, weighted sample, raw sample
ebalance agegr18_24 agegr25_44 agegr45_64 sex lessThanHighSchool highSchoolDegree someCollege bachelor, manualtargets(.126 .342 .339 .487 .122 .296 .284 .192)

*Figure A1: Unweighted vs. weighted
reg relief_ i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote, cluster(uniqueID) robust //unweighted
matrix main_weighted = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(main_weighted) saving(main_weighted) replace

reg relief_ i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote [pweight = weight], cluster(uniqueID) robust //weighted 
matrix main_unweighted = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(main_unweighted) saving(main_unweighted) replace

*Figure A2: Likely voters vs. Adult Population (weighted)
reg relief_ i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote [pweight = weight],  cluster(uniqueID) robust 
matrix main = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(main) saving(olscon_main) replace

reg relief_ i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote [pweight = weightLikelyVoter],  cluster(uniqueID) robust // Voting population
matrix main = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(main) saving(olscon_main_votingpop) replace

*Figure A3: By affectedness
gen aff_index = (roads_m+roads_h+school_m+school_h+comm_m+comm_h+food_m+food_h+evacuation_m+evacuation_h+finloss_m+finloss_h ///
					+ injury_m + injury_h)/14
gen aff_groups = 0 if aff_index==0
replace aff_groups = 1 if aff_index > 0 & aff_index < 0.3
replace aff_groups = 2 if aff_index >0.3
replace aff_groups = . if aff_index ==.

gen aff_high = 1 if aff_index != 0 //affected
replace aff_high = 0 if aff_index ==0 //not affected

reg relief_ i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote [pweight = weight] if aff_high==1,  cluster(uniqueID) robust  //affected
matrix aff_high= (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(aff_high) saving(olscon_aff_high) replace

reg relief_ i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote [pweight = weight] if aff_high==0,  cluster(uniqueID) robust  //not affected
matrix aff_low = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(aff_low) saving(olscon_aff_low) replace

*Figure A4: By attentiveness 
reg relief_ i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote [pweight = weight] if color_correct==1,  cluster(uniqueID) robust //Attention question correct: Yes
matrix aff_high= (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(aff_high) saving(olscon_att_high) replace

reg relief_ i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote [pweight = weight] if color_correct==0,  cluster(uniqueID) robust //Attention question correct: No
matrix aff_low = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(aff_low) saving(olscon_att_low) replace

*Figure A5: By completion time
forvalues i = 1/6{
gen sum_`i' = relcon`i'1_timer_3 + relcon`i'2_timer_3 + relcon`i'3_timer_3 + relcon`i'4_timer_3
}

gen con_time = sum_1
replace con_time = sum_2 if con_time == .
replace con_time = sum_3 if con_time == .
replace con_time = sum_4 if con_time == .
replace con_time = sum_5 if con_time == .
replace con_time = sum_6 if con_time == .
gen con_timem = con_time/60

sort con_timem
gen seq_con_timem = _n
gen con_timem_high = (seq_con_timem > 20944/2)

reg relief_ i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote  [pweight = weight] if con_timem_high == 1,  cluster(uniqueID) robust  //long time
matrix time_a = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(time_a) saving(time_a) replace

reg relief_ i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote [pweight = weight] if con_timem_high == 0,  cluster(uniqueID) robust  //little time
matrix time_d = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(time_d) saving(time_d) replace

*Figure A6: By race
reg relief_ i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote [pweight = weight] if race == 1,  cluster(uniqueID) robust //white
matrix white = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(white) saving(ols_white) replace

reg relief_ i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote [pweight = weight] if race != 1,  cluster(uniqueID) robust //non-white 
matrix nonwhite = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(nonwhite) saving(ols_nonwhite) replace

*Figure A7: By partisan identification 
gen southern = (state==1 | state==108 | state==317 | state==321 | state==389 | state==1002 | state==1123 | state==1205 | state==1417 | state==1914 | state==2158 | state==2347 | state==2461 | state==2557 | state==2857 | state==2993)
gen southern_republican = (partyID == 1 & southern==1)
gen southern_dems = (partyID == 2 & southern==1)

reg relief_ i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote [pweight = weight] if southern_republican == 1,  cluster(uniqueID) robust  
matrix southern_rep = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(southern_rep) saving(ols_southern_rep) replace

reg relief_ i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote [pweight = weight] if southern_dems == 1,  cluster(uniqueID) robust  
matrix southern_dems = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(southern_dems) saving(ols_southern_dems) replace

*Figure A8: Tobit estimates
tobit relief_ i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote [pweight = weight], ll(0) ul(10) cluster(uniqueID) robust 
matrix main = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(main) saving(olscon_main_tobit) replace

*Figure A9: Observed vs. preferred (conjoint: Democratic president)
reg log_relief i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote [pweight = weight] if FeatPresident == 1,  cluster(uniqueID) robust 
matrix pres_dem = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(pres_dem) saving(olspres_dem_log) replace

*Figure A10: z-transformed relief amount
gen relief_z = relief_/10
gen zrelief = relief_z
replace zrelief = relief_z + 0.01 if relief_z==0
replace zrelief = relief_z - 0.01 if relief_z==1
egen relief_ztransformed = std(zrelief)

reg relief_ztransformed i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote [pweight = weight],  cluster(uniqueID) robust 
matrix szenario = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(szenario) saving(ztransformed) replace

*Figure A11: Main model vs. only one scenario (note: this drops half of the observations so the previous code will not be valid anymore)
drop if scenario == 1
reg relief_ i.FeatDamage i.FeatDead inc70 inc40 inc10 i.FeatUnemp i.FeatPresVote [pweight = weight],  cluster(uniqueID) robust 
matrix szenario = (e(b) \ vecdiag(e(V)))'
mat2txt, matrix(szenario) saving(szenario) replace


